"""Application Configuration"""
import os
from pathlib import Path
from dotenv import load_dotenv

# 加载环境变量
load_dotenv()


class Settings:
    """应用配置类"""
    
    # OpenAI API 配置
    OPENAI_API_KEY: str = os.getenv("OPENAI_API_KEY", "")
    OPENAI_BASE_URL: str = os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1")
    
    # 模型配置
    EMBEDDING_MODEL: str = os.getenv("EMBEDDING_MODEL", "text-embedding-3-small")
    CHAT_MODEL: str = os.getenv("CHAT_MODEL", "gpt-4o-mini")
    
    # 应用配置
    CHUNK_SIZE: int = int(os.getenv("CHUNK_SIZE", "500"))
    TOP_K: int = int(os.getenv("TOP_K", "3"))
    MAX_FILE_SIZE: int = int(os.getenv("MAX_FILE_SIZE", "10485760"))  # 10MB
    
    # 文件路径配置
    BASE_DIR: Path = Path(__file__).resolve().parent.parent.parent
    DATA_DIR: Path = BASE_DIR / "data"
    UPLOADS_DIR: Path = DATA_DIR / "uploads"
    VECTOR_STORE_PATH: Path = DATA_DIR / "vector_store.json"
    
    def __init__(self):
        """初始化配置，确保目录存在"""
        self.DATA_DIR.mkdir(exist_ok=True)
        self.UPLOADS_DIR.mkdir(exist_ok=True)
        
        # 初始化向量存储文件
        if not self.VECTOR_STORE_PATH.exists():
            import json
            with open(self.VECTOR_STORE_PATH, "w", encoding="utf-8") as f:
                json.dump({
                    "documents": [],
                    "metadata": {
                        "total_documents": 0,
                        "total_chunks": 0,
                        "last_updated": None
                    }
                }, f, ensure_ascii=False, indent=2)


# 创建全局配置实例
settings = Settings()

